﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Manager.CustomerHelper;
using ExtAspNet;
namespace HealthSystem
{
    public partial class HealthMain : BasePage
    {
        //public ExtAspNet.Tree treeMenu = new Tree();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CustomerProfile.RoleId.ToString().StartsWith("1"))
                {
                    unit.Text = CustomerProfile.AdminUser.hW_Admin_Detail.WorkUnit;
                    if (CustomerProfile.RoleId == 1003)
                    {
                        Tab1.IFrameUrl = "~/MySchool.aspx?districtid=" + CustomerProfile.DistrictId;
                    }
                }
                if (CustomerProfile.RoleId == 2000)
                {
                    unit.Text = CustomerProfile.OrgUser.hw_Org.OrgName;
                    Tab1.IFrameUrl = "~/MySchool.aspx?orgcode=" + CustomerProfile.OrgUser.OrgCode;
                }
                if (CustomerProfile.RoleId == 3000)
                {
                    unit.Text = CustomerProfile.SchoolUser.hw_School.Name;
                }
                name.Text = User.Identity.Name;
                InitMenu();
            }
        }
        protected void NodeAction(object sender, ExtAspNet.TreeCommandEventArgs e)
        {
            //var m = this.treeMenu.Nodes;
            string biao = e.Node.NodeID;
            string caidan = e.Node.Text;
            PageContext.RegisterStartupScript(mainTabStrip.GetAddTabReference(biao, e.Node.Target, caidan, IconHelper.GetIconUrl(Icon.HouseConnect), true));

        }
        private void InitMenu()
        {
            //ExtAspNet.Tree treeMenu = new Tree();
            //treeMenu.AutoLeafIdentification = false;
            //treeMenu.NodeCommand += new EventHandler<TreeCommandEventArgs>(NodeAction);
            //treeMenu.ID = "treeMenu";
            //regionLeft.Items.Add(treeMenu);

            //ExtAspNet.TreeNodeCollection coll = new ExtAspNet.TreeNodeCollection();
            //coll.Add(
            
            //XMenuCollection menus = new Select().From<XMenu>()
            //    //.Where(XMenu.IsMainMenuColumn).IsEqualTo(true)
            //    .OrderAsc(XMenu.SortIndexColumn.ColumnName)
            //    .ExecuteAsCollection<XMenuCollection>();

            List<CustomMenu> menus = new List<CustomMenu>();
            List<CustomMenu> originalMenus = CustomMenuHelper.GetCustomMenus();
            //treeMenu.Nodes.Clear();
            // 当前角色可以显示的菜单列表
            List<int> menuIds = RoleMenuHelper.GetRoleMenus(GetIdentityRoleId());
            foreach (CustomMenu menu in originalMenus)
            {
                if (menuIds.Contains(menu.Id))
                {
                    menus.Add(menu);
                }
            }
            ExtAspNet.Accordion accordion = new Accordion();
            accordion.ShowHeader = false;


            ResolveMenuTree(menus, 0, treeMenu1.Nodes);
            //ResolveMenuTree(menus, 1, treeMenu2.Nodes);
            //ResolveMenuTree(menus, 5, treeMenu3.Nodes);
            //ResolveMenuTree(menus, 6, treeMenu4.Nodes);
            //for (int i = 0; i < menus.Count; i++)
            //{
            //    CustomMenu m = menus[i];
            //    if (m.ParentId == 0)
            //    {
            //        panel = new AccordionPane();
            //        panel.Title = m.Name;
            //        ExtAspNet.Tree treeMenu = new Tree();
            //        treeMenu.ShowHeader = false;
            //        treeMenu.ShowBorder = false;
            //        ResolveMenuTree(menus, m.Id, treeMenu.Nodes);
            //        treeMenu.NodeCommand += new EventHandler<TreeCommandEventArgs>(NodeAction);
            //        panel.Items.Add(treeMenu);
            //        accordion.Panes.Add(panel);
            //    }
            //}
            //regionLeft.Items.Add(accordion);
            // 生成树
           // ResolveMenuTree(menus, 0, treeMenu.Nodes);

            //if (treeMenu.Nodes.Count == 0)
            //{
            //    Response.Write("系统管理员尚未给你配置菜单！");
            //    Response.End();

            //    return;
            //}

            // //展开第一个树节点
            //treeMenu.Nodes[0].Expanded = true;

            #region old code
            //if (XConfigHelper.MenuType.ToLower() == "accordion")
            //{
            //    Accordion accordion = new Accordion();
            //    regionLeft.Items.Add(accordion);
            //    accordion.ShowBorder = false;
            //    accordion.ShowHeader = false;

            //    foreach (MyMenu menu in newMenus)
            //    {
            //        if (menu.ParentMenuId == 0)
            //        {
            //            AccordionPane pane = new AccordionPane();
            //            accordion.Panes.Add(pane);
            //            pane.Title = menu.Name;
            //            pane.ShowBorder = false;

            //            Tree tree = new Tree();
            //            pane.Items.Add(tree);
            //            tree.ShowBorder = false;
            //            tree.ShowHeader = false;
            //            tree.AutoScroll = true;
            //            tree.EnableArrows = true;

            //            ResolveMenuTree(newMenus, menu.Id, tree.Nodes);
            //        }
            //    }
            //}
            //else
            //{
            //    Tree tree = new Tree();
            //    regionLeft.Items.Add(tree);
            //    tree.ShowBorder = false;
            //    tree.ShowHeader = false;
            //    tree.AutoScroll = true;
            //    tree.EnableArrows = true;

            //    ResolveMenuTree(newMenus, 0, tree.Nodes);
            //} 
            #endregion
        }
        /// <summary>
        /// 生成菜单树
        /// </summary>
        /// <param name="menus"></param>
        /// <param name="parentMenuId"></param>
        /// <param name="nodes"></param>
        private void ResolveMenuTree(List<CustomMenu> menus, int parentMenuId, ExtAspNet.TreeNodeCollection nodes)
        {
            foreach (CustomMenu menu in menus)
            {
                if (menu.ParentId == parentMenuId)
                {
                    ExtAspNet.TreeNode node = new ExtAspNet.TreeNode();
                    

                    node.Text = menu.Title;
                    node.IconUrl = menu.ImageUrl;
                    if (!String.IsNullOrEmpty(menu.NavigateUrl))
                    {
                        node.EnablePostBack = true;
                        //node.EnablePostBack = true;
                        //node.AutoPostBack = true;
                        node.Target = ResolveUrl(menu.NavigateUrl);
                        //PageContext.RegisterStartupScript(mainTabStrip.GetAddTabReference(node.NodeID, node.NavigateUrl, node.Text, IconHelper.GetIconUrl(Icon.HouseConnect), true));
                        //node.OnClientClick = String.Format("addTab('{0}','{1}','{2}')", node.NodeID, ResolveUrl(menu.NavigateUrl), node.Text.Replace("'", ""));
                    }
                    if (menu.IsTreeLeaf)
                    {
                        node.Leaf = true;
                    }
                    
                    //else
                    //{
                    //    node.Expanded = true;
                    //}
                    nodes.Add(node);
                    //node.EnablePostBack = true;
                    nodes[0].Expanded = true;
                    ResolveMenuTree(menus, menu.Id, node.Nodes);
                    
                }
            }
        }
        protected void btnExit_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            Response.Redirect("~/index.html");
        }
    }
}